home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261exe.zip / IMPATH.PS < prev    next >
Text File  |  1993-05-28  |  6KB  |  181 lines

  1. %    Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2. %
  3. % This file is part of Ghostscript.
  4. %
  5. % Ghostscript is distributed in the hope that it will be useful, but
  6. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. % to anyone for the consequences of using it or for whether it serves any
  8. % particular purpose or works at all, unless he says so in writing.  Refer
  9. % to the Ghostscript General Public License for full details.
  10. %
  11. % Everyone is granted permission to copy, modify and redistribute
  12. % Ghostscript, but only under the conditions described in the Ghostscript
  13. % General Public License.  A copy of this license is supposed to have been
  14. % given to you along with Ghostscript so you can know your rights and
  15. % responsibilities.  It should be in a file named COPYING.  Among other
  16. % things, the copyright notice and this notice must be preserved on all
  17. % copies.
  18.  
  19. % impath.ps
  20. % Reverse-rasterize a bitmap to produce a Type 1 outline.
  21. % (This was formerly a Ghostscript operator implemented in C.)
  22.  
  23. %    <image> <width> <height> <wx> <wy> <ox> <oy> <string>
  24. %      type1imagepath <substring>
  25. %        Converts an image (bitmap) description of a character into
  26. %          a scalable description in Adobe Type 1 format.  The
  27. %          current transformation matrix should be the same as the
  28. %          FontMatrix of the font in which this character will be
  29. %          used: this establishes the scaling relationship between
  30. %          image pixels (the image is assumed to be 1 unit high in
  31. %          user space) and the character coordinate system used in
  32. %          the scalable description.  wx and wy are the character
  33. %          width, and ox and oy are the character origin relative
  34. %          to the lower left corner of the bitmap, in *pixels*.
  35. %        The image is assumed to be stored in left-to-right,
  36. %          top-to-bottom order.  Note that this is not consistent
  37. %          with the `image' operator's interpretation of the CTM.
  38. %        All coordinates in the scalable description are rounded to
  39. %          integers, so the coefficients in the FontMatrix should
  40. %          be on the order of 1/N for some value of N that is
  41. %          either a multiple of the height/width or is large
  42. %          compared to the width and height.  (There is a
  43. %          convention, which some PostScript programs rely on, that
  44. %          N=1000.)
  45. %        Note that the encoded description has *not* been subjected
  46. %          to CharString encryption, which is necessary before the
  47. %          description can be given to type1addpath: to do this,
  48. %          follow the type1imagepath with
  49. %            4330 exch dup .type1encrypt exch pop
  50. %        If the description is too complex to fit into the supplied
  51. %          string, a limitcheck error results.  A good rule of
  52. %          thumb is that the size of the string should be about 6
  53. %          times the number of 1-bits in the image that are not
  54. %          completely surrounded by other 1-bits.
  55.  
  56. % Import the Type 1 opcodes.
  57. (type1ops.ps) run
  58.  
  59. 100 dict
  60. dup /type1imagepath_dict exch def
  61. begin
  62.  
  63. /rc { round cvi } bind def
  64. /moving [/rmoveto /hmoveto /vmoveto] def
  65. /drawing [/rlineto /hlineto /vlineto] def
  66.  
  67. % Convert the current path to a Type 1 token array.
  68. /putxy            % x y ops -> cs_elements
  69.  { 3 -1 roll dup x sub rc exch /x exch def
  70.    3 -1 roll dup y sub rc exch /y exch def
  71.    % stack: ops dx dy
  72.    dup 0 eq
  73.     { % dy = 0, use hmoveto/lineto
  74.       pop exch 1 get
  75.     }
  76.     { 1 index 0 eq
  77.        { % dx = 0, use vmoveto/lineto
  78.          exch pop exch 2 get
  79.        }
  80.        { % use rmoveto/rlineto
  81.          3 -1 roll 0 get
  82.        }
  83.       ifelse
  84.     }
  85.    ifelse
  86.  } bind def
  87. /pathtotype1        % -> charstack
  88.  { 3 dict begin /x 0 def /y 0 def
  89.    mark
  90.    { moving putxy
  91.    }
  92.    { drawing putxy
  93.    }
  94.    { % Convert curve to relative form
  95.      x y 3
  96.       { exch neg 7 index add rc
  97.         exch neg 6 index add rc
  98.     8 -2 roll
  99.       }
  100.      repeat /y exch def /x exch def
  101.      1 index 0 eq 5 index 0 eq and    % dy1=dx3=0, hv
  102.       { 5 -1 roll pop exch pop /hvcurveto
  103.       }
  104.       { dup 0 eq 6 index 0 eq and    % dx1=dy3=0, vh
  105.          { 6 -1 roll pop pop /vhcurveto
  106.      }
  107.      { /rrcurveto            % none of the above
  108.      }
  109.     ifelse
  110.       }
  111.      ifelse
  112.    }
  113.    { /closepath
  114.    }
  115.    pathforall end
  116.  } bind def
  117.  
  118. end    % type1imagepath_dict
  119.  
  120. % The main program
  121. /type1imagepath        % image width height wx wy ox oy string ->
  122.             % substring
  123.  { type1imagepath_dict begin
  124.    /tsave save def
  125.    /ostring exch def
  126.    /oy exch def   /ox exch def
  127.    /wy exch def   /wx exch def
  128.    /height exch def   /width exch def
  129.    /data exch def
  130.  
  131.    /ofilter ostring /NullEncode filter def
  132.    /raster width 7 add 8 idiv def
  133.  
  134. % Construct the coordinate transformation.
  135.    height dup scale
  136.      matrix currentmatrix matrix invertmatrix setmatrix
  137.  
  138. % Determine the left side bearing.
  139.    /lsbx width
  140.    0 1 width 1 sub
  141.     { dup dup 8 idiv 0 exch
  142.       raster raster height mul 1 sub
  143.        { data exch get or }
  144.       for exch 8 mod bitshift 128 and 0 ne
  145.        { exch pop exit }
  146.       if pop
  147.     }
  148.    for def
  149.  
  150. % Compute and encode the origin, width, and side bearing.
  151.    mark
  152.    ox oy dtransform
  153.      rc /opty exch def   rc /optx exch def
  154.    wx wy dtransform
  155.      rc /iwy exch def   rc /iwx exch def
  156.    lsbx ox sub 0 dtransform
  157.      rc /ilsby exch def   rc /ilsbx exch def
  158.    ilsbx
  159.    iwy 0 ne ilsby 0 ne or
  160.     { ilsby iwx iwy /sbw }
  161.     { iwx /hsbw }
  162.    ifelse
  163.    ofilter charstack_write
  164.  
  165. % Flip over the Y axis, because the image is stored top-to-bottom.
  166.    [1 0 0 -1 0 height] concat
  167. % Account for the character origin.
  168.    ox lsbx sub oy translate
  169. % Trace the outline.
  170.    newpath
  171.    width height data .imagepath
  172.    gsave matrix setmatrix pathtotype1 grestore
  173.    ofilter charstack_write
  174. % Terminate the output
  175.    mark /endchar ofilter charstack_write
  176.  
  177.    ostring 0 ofilter fileposition getinterval
  178.    tsave restore
  179.    end
  180.  } bind def
  181.